/**
 * Created by gm on 2016/7/28.
 */

var loginApp = angular.module('login-messageapi',
    ['ngResource', 'ngRoute', 'ngCookies']);
loginApp.config(['$routeProvider', function ($routeProvider) {
  $routeProvider
  .when('/', {
    templateUrl: 'partials/login.html',
    controller: 'LoginCtrl'
  })
  .otherwise({redirectTo: '/'});
}]);
//登录验证
loginApp.controller('LoginCtrl',
    ['$scope', '$resource', '$cookies', '$routeParams',
      function ($scope, $resource, $cookies, $routeParams) {
        $scope.login = function () {
          var psw = $scope.password;
          var hash = hex_md5(psw);
          var hash2 = hex_md5("c7vq89kh" + hash);
          var Mongo = $resource('/administrator/login');
          var postParams = {username: $scope.username};
          Mongo.save(postParams, function (user) {
            if (user._id != null) {
              if (hash2 == user.password) {
                $cookies.put('id', user._id);
                $cookies.put('username', user.username);
                $cookies.put('appId', user.appid);
                if (user.username == 'admin') {
                  window.location = "/#/index";
                } else {
                  window.location = "/send";
                }
              } else if ($scope.password != undefined) {
                $scope.password = '';
                alert('密码有误，请重新输入');
              }
            } else {
              $scope.username = '';
              alert(user.msg);
            }
          });
        }
      }]);
var sendApp = angular.module('sendmsgs',
    ['ngResource', 'ngRoute', 'ngCookies']);

sendApp.config(['$routeProvider', function ($routeProvide) {
  $routeProvide
  .when('/', {
    templateUrl: 'partials/send.html',
    controller: 'SendCtrl'
  })
  .otherwise({redirectTo: '/'});
}]);
//多条推送
sendApp.controller('SendCtrl', ['$scope', '$resource', '$location', '$cookies',
  function ($scope, $resource, $location, $cookies) {
    $scope.title = "短信群发";
    var currentUser = $cookies.get('username');
    if (currentUser == 'null' || currentUser == undefined) {
      window.location = '/login';
    } else {
      $scope.username = currentUser;
    }
    $scope.logout = function () {
      $cookies.put('id', null);
      $cookies.put('username', null);
      $cookies.put('appId',null);
      window.location = '/login';
    };
    $scope.send = function () {
      var mobile=$scope.mobiles;
      if (mobile.length > 1200) {
        alert('最多不超过100个手机号');
      } else {
        var cont = $scope.content;
        var contentUrl = encodeURIComponent(cont);
        var re = /%20/g;
        var content = contentUrl.replace(re, "+");
        var appId = $cookies.get("appId");
        var Mongo = $resource('/manage/userInfo');
        Mongo.get({appId: appId}, function (data) {
          var key = data.secretKey;
          var md5Str = key + mobile + content;
          var md5 = hex_md5(md5Str);
          var postParams = 'appId='+appId+'&content=' + content + '&mobile=' + mobile
              + '&code=' + md5;
          var severHttp = 'http://sms.site.k618.cn/send';
          $.post(severHttp, postParams, function (rs) {
            var result = rs;
            console.log(result.status);
            if (result.status == 0) {
              alert('发送成功');
            } else {
              alert('发送失败');
            }
          });
          $scope.mobiles="";
          $scope.content="";
        });

      }

    };
  }]);
var app = angular.module('messageapi', ['ngResource', 'ngRoute', 'ngCookies']);

app.config(['$routeProvider', function ($routeProvider) {
  $routeProvider
  .when('/index', {
    templateUrl: 'partials/messageInfo.html',
    controller: 'HomeCtrl'
  })
  .when('/statistic', {
    templateUrl: 'partials/statistic.html',
    controller: 'StatisticCtrl'
  })
  .when('/addUser', {
    templateUrl: 'partials/addUser.html',
    controller: 'AddUserCtrl'
  })
  .when('/userinfo', {
    templateUrl: 'partials/manageUsers.html',
    controller: 'UsersCtrl'
  })
  .when('/deleteUser/:id', {
    templateUrl: 'partials/deleteUser.html',
    controller: 'DeleteUserCtrl'
  })
  .when('/resetPsw/:id', {
    templateUrl: 'partials/resetPsw.html',
    controller: 'ResetPswCtrl'
  })
  .when('/editUser/:id', {
    templateUrl: 'partials/addUser.html',
    controller: 'EditUserCtrl'
  })
  .when('/usable', {
    templateUrl: 'partials/manageUsers.html',
    controller: 'UsableCtrl'
  })
  .when('/expired', {
    templateUrl: 'partials/manageUsers.html',
    controller: 'ExpiredCtrl'
  })
  .when('/unused', {
    templateUrl: 'partials/manageUsers.html',
    controller: 'UnusedCtrl'
  })
  .when('/upMessage', {
    templateUrl: 'partials/upMessage.html',
    controller: 'UpMessageCtrl'
  })
  .when('/sendUsers',{
    templateUrl:'partials/sendUsers.html',
    controller:'SendUsersCtrl'
  })
  .when('/addSendUser',{
    templateUrl:'partials/addSendUser.html',
    controller:'AddSendUserCtrl'
  })
  .when('/deleteSendUser/:id',{
    templateUrl:'partials/deleteSendUser.html',
    controller:'DeleteSendUserCtrl'
  })
  .otherwise({redirectTo: '/index'});
}]);

//模板部分验证用户是否登录
app.controller('ParentCtrl', ['$scope', '$resource', '$location', '$cookies',
  function ($scope, $resource, $location, $cookies) {
    var currentUser = $cookies.get('username');
    if (currentUser == 'null' || currentUser == undefined) {
      window.location = '/login';
    } else {
      $scope.username = currentUser;
    }
    $scope.logout = function () {
      $cookies.put('id', null);
      $cookies.put('username', null);
      $cookies.put('appId',null);
      window.location = '/login';
    };
  }]);
//短信发送情况
app.controller('HomeCtrl', ['$scope', '$resource', '$location', '$cookies',
  function ($scope, $resource, $location, $cookies) {
    var username = $cookies.get('username');
    if (username == 'admin') {
      $scope.title = "短信发送情况";
      $scope.name = 'smsSend';
      $scope.first = true;
      $scope.pages = 0;
      $scope.pageSize = 15;
      $scope.p_current = 1;
      $scope.pageList = [];
      var Mongo = $resource('/messageInfo');
      //第一次进入页面
      setData($scope.name, $scope.pageSize, $scope.p_current, $scope.first,
          $scope.content);
      //设置分页
      function setData(name, pageSize, p_current, first, content) {
        var postParams = {
          "name": name,
          "pageSize": pageSize,
          "p_current": p_current,
          "content": content
        };
        Mongo.save(postParams, function (data) {
          $scope.pages = Math.ceil(data.count / $scope.pageSize);
          $scope.items = data.result;
          if (first) {
            $scope.selectPage(p_current);
            $scope.first = false;
          }
        });
      }

      //点击页码跳转
      $scope.selectPage = function (page) {
        //不能小于1大于最大
        if (page < 1 || page > $scope.pages) {
          return;
        }
        //最多显示分页数5
        var newpageList = [];
        if (page < 3) {
          if ($scope.pages < 6) {
            for (var i = 0; i < $scope.pages; i++) {
              newpageList.push(i + 1);
            }
          } else {
            for (var i = 0; i < 5; i++) {
              newpageList.push(i + 1);
            }
          }
        } else {
          for (var i = (page - 3);
              i < ((page + 2) > $scope.pages ? $scope.pages : (page + 2));
              i++) {
            newpageList.push(i + 1);
          }
        }
        $scope.pageList = newpageList;
        $scope.p_current = page;
        if (!$scope.first) {
          setData($scope.name, $scope.pageSize, $scope.p_current, $scope.first,
              $scope.content);
        }
        $scope.isActivePage = function (page) {
          return $scope.p_current == page;
        };
      };
      //首页
      $scope.First = function () {
        $scope.selectPage(1);
      };
      //上一页
      $scope.Previous = function () {
        $scope.selectPage($scope.p_current - 1);
      };
      //下一页
      $scope.Next = function () {
        $scope.selectPage($scope.p_current + 1);
      };
      //尾页
      $scope.Last = function () {
        $scope.selectPage($scope.pages);
      };
      //按商户号、内容、手机号等进行模糊查询
      $scope.search = function () {
        if ($scope.content != undefined || $scope.content != '') {
          $scope.name = 'searchMessage';
        }
        $scope.p_current = 1;
        $scope.first = true;
        setData($scope.name, $scope.pageSize, $scope.p_current, $scope.first,
            $scope.content);
      };
    } else {
      window.location = "/send";
    }

  }]);
//信息发送统计
app.controller('StatisticCtrl', ['$scope', '$resource', '$location', '$cookies',
  function ($scope, $resource, $location, $cookies) {
    var username = $cookies.get('username');
    if (username == 'admin') {
      $scope.p_current = 1;
      $scope.name = 'statistic';
      var startTime, endTime;
      var Messages = $resource('/messageInfo');
      var postParams = {
        name: $scope.name,
        appId: $scope.appId,
        startTime: startTime,
        endTime: endTime
      };
      Messages.save(postParams, function (result) {
        $scope.items = result.data;
        setPages($scope.items);
      });
      $('#datetimepicker1').datetimepicker({
        minView: 'month',
        format: 'yyyy-mm-dd',
        language: 'zh-CN',
        autoclose: true
      }).on('dp.change', function (e) {
        var result = new e.date.format('yyyy-mm-dd');
        $scope.startTime = result;
      });
      $('#datetimepicker2').datetimepicker({
        minView: 'month',
        format: 'yyyy-mm-dd',
        language: 'zh-CN',
        autoclose: true
      }).on('dp.change', function (e) {
        var result = new e.date.format('yyyy-mm-dd');
        $scope.startTime = result;
      });
      function setPages(data) {
        $scope.pageSize = 15;
        $scope.pages = Math.ceil(data.length / $scope.pageSize);
        $scope.newPages = $scope.pages > 5 ? 5 : $scope.pages;
        $scope.pageList = [];
        $scope.selPage = 1;
        //设置表格数据源（分页）
        $scope.setData = function () {
          $scope.items = data.slice(($scope.pageSize * ($scope.selPage - 1)),
              $scope.selPage * $scope.pageSize);//通过当前页数筛选出表格当前显示数据
        };
        $scope.items = data.slice(0, $scope.pageSize);
        //分页要repeat的数组
        for (var i = 0; i < $scope.newPages; i++) {
          $scope.pageList.push(i + 1);
        }
        //打印当前选中页索引
        $scope.selectPage = function (page) {
          //不能小于1大于最大
          if (page < 1 || page > $scope.pages) {
            return;
          }
          //最多显示分页数5
          var newpageList = [];
          if (page < 3) {
            if ($scope.pages < 6) {
              for (var i = 0; i < $scope.pages; i++) {
                newpageList.push(i + 1);
              }
            } else {
              for (var i = 0; i < 5; i++) {
                newpageList.push(i + 1);
              }
            }
          } else {
            for (var i = (page - 3);
                i < ((page + 2) > $scope.pages ? $scope.pages : (page + 2));
                i++) {
              newpageList.push(i + 1);
            }
          }
          $scope.pageList = newpageList;
          $scope.selPage = page;
          $scope.setData();
        };
        //设置当前选中页样式
        $scope.isActivePage = function (page) {
          return $scope.selPage == page;
        };
        //首页
        $scope.First = function () {
          $scope.selectPage(1);
        };
        //上一页
        $scope.Previous = function () {
          $scope.selectPage($scope.selPage - 1);
        };
        //下一页
        $scope.Next = function () {
          $scope.selectPage($scope.selPage + 1);
        };
        //尾页
        $scope.Last = function () {
          $scope.selectPage($scope.pages);
        };
      }

      $scope.query = function () {
        if ($scope.startTime != undefined) {
          startTime = $scope.startTime + " 00:00:00";
        } else {
          startTime = undefined;
        }
        if ($scope.endTime != undefined) {
          endTime = $scope.endTime + " 23:59:59";
        } else {
          endTime = undefined;
        }
        var postParams = {
          name: $scope.name,
          appId: $scope.appId,
          startTime: startTime,
          endTime: endTime
        };
        Messages.save(postParams, function (result) {
          $scope.items = result.data;
          setPages($scope.items);
        });
      };
      //首页
      $scope.First = function () {
        $scope.selectPage(1);
      };
      //上一页
      $scope.Previous = function () {
        $scope.selectPage($scope.p_current - 1);
      };
      //下一页
      $scope.Next = function () {
        $scope.selectPage($scope.p_current + 1);
      };
      //尾页
      $scope.Last = function () {
        $scope.selectPage($scope.pages);
      };
      $scope.reset = function () {
        $scope.appId = undefined;
        $scope.startTime = undefined;
        $scope.endTime = undefined;
      };
    } else {
      window.location = "/send";
    }
  }]);
//添加商户
app.controller('AddUserCtrl', ['$scope', '$resource', '$location', '$cookies',
  function ($scope, $resource, $location, $cookies) {
    var username = $cookies.get('username');
    if (username == 'admin') {
      $scope.title = '新增商户';
      $scope.disabled = false;
      $scope.name = "addClient";
      function randomWord(randomFlag) {
        var str = "",
            arr = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b',
              'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o',
              'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'];
        // 随机产生
        for (var i = 0; i < randomFlag; i++) {
          var pos = Math.round(Math.random() * (arr.length - 1));
          str += arr[pos];
        }
        return str;
      }

      $scope.createSecretKey = function () {
        $scope.secretKey = randomWord(15);
      };
      $scope.options = [
        {key: '待使用', id: '0'},
        {key: '合同期内', id: '1'},
        {key: '合同过期', id: '2'}
      ];
      $scope.save = function () {
        var appId = $scope.appId;
        var secretKey = $scope.secretKey;
        if (appId != undefined) {
          var Mongo = $resource('/manage');
          if (appId != '' && secretKey != '' && $scope.status.id != '') {
            var newUser = {
              name: $scope.name,
              appId: $scope.appId,
              secretKey: $scope.secretKey,
              status: $scope.status.id,
              checkContent: $scope.checkContent,
              remark: $scope.remark
            };
            Mongo.save(newUser, function (data) {
              if (data.errNum == 0) {
                alert(data.message);
                $scope.appId = '';
              } else if (data.errNum == 1) {
                alert(data.message);
                $scope.secretKey = '';
              } else {
                alert("添加成功");
                $location.path('/userinfo');
              }
            });
          }
        }
      };
    } else {
      window.location = "/send";
    }
  }]);
//删除商户
app.controller('DeleteUserCtrl', ['$scope', '$resource', '$location', '$routeParams', '$cookies',
      function ($scope, $resource, $location, $routeParams, $cookies) {
        var username = $cookies.get('username');
        if (username == 'admin') {
          var Mongo = $resource('/manage/user/:id');
          Mongo.get({id: $routeParams.id}, function (user) {
            $scope.user = user;
          });
          $scope.delete = function () {
            Mongo.delete({id: $routeParams.id}, function () {
              alert("删除成功");
              $location.path('/userinfo');
            });
          };
        } else {
          window.location = "/send";
        }
      }]);
//商户管理
app.controller('UsersCtrl', ['$scope', '$resource', '$location', '$cookies',
  function ($scope, $resource, $location, $cookies) {
    var username = $cookies.get('username');
    if (username == 'admin') {
      $scope.name = 'scanClient';
      $scope.title = '所有商户信息';
      $scope.first = true;
      $scope.pages = 0;
      $scope.pageSize = 15;
      $scope.p_current = 1;
      $scope.pageList = [];
      var Mongo = $resource('/manage');
      //第一次进入页面
      setData($scope.name, $scope.pageSize, $scope.p_current, $scope.first,
          $scope.content);
      //设置分页
      function setData(name, pageSize, p_current, first, content) {
        var postParams = {
          "name": name,
          "pageSize": pageSize,
          "p_current": p_current,
          "content": content
        };
        Mongo.save(postParams, function (data) {
          $scope.pages = Math.ceil(data.count / $scope.pageSize);
          $scope.items = data.result;
          if (first) {
            $scope.selectPage(p_current);
            $scope.first = false;
          }
        });
      }

      //点击页码跳转
      $scope.selectPage = function (page) {
        //不能小于1大于最大
        if (page < 1 || page > $scope.pages) {
          return;
        }
        //最多显示分页数5
        var newpageList = [];
        if (page < 3) {
          if ($scope.pages < 6) {
            for (var i = 0; i < $scope.pages; i++) {
              newpageList.push(i + 1);
            }
          } else {
            for (var i = 0; i < 5; i++) {
              newpageList.push(i + 1);
            }
          }
        } else {
          for (var i = (page - 3);
              i < ((page + 2) > $scope.pages ? $scope.pages : (page + 2));
              i++) {
            newpageList.push(i + 1);
          }
        }
        $scope.pageList = newpageList;
        $scope.p_current = page;
        if (!$scope.first) {
          setData($scope.name, $scope.pageSize, $scope.p_current, $scope.first,
              $scope.content);
        }
        $scope.isActivePage = function (page) {
          return $scope.p_current == page;
        };
      };
      //首页
      $scope.First = function () {
        $scope.selectPage(1);
      };
      //上一页
      $scope.Previous = function () {
        $scope.selectPage($scope.p_current - 1);
      };
      //下一页
      $scope.Next = function () {
        $scope.selectPage($scope.p_current + 1);
      };
      //尾页
      $scope.Last = function () {
        $scope.selectPage($scope.pages);
      };
      //按商户号、内容、手机号等进行模糊查询
      $scope.search = function () {
        if ($scope.content != undefined || $scope.content != '') {
          $scope.name = 'searchClient';
        }
        $scope.p_current = 1;
        $scope.first = true;
        setData($scope.name, $scope.pageSize, $scope.p_current, $scope.first,
            $scope.content);
      };
    } else {
      window.location = "/send";
    }
  }]);
//修改密码
app.controller('ResetPswCtrl', ['$scope', '$resource', '$location', '$routeParams','$cookies',
  function ($scope, $resource, $location,$routeParams, $cookies) {
    var username = $cookies.get('username');
    if (username == 'admin') {
      var userInfo = '';
      var currentId = $routeParams.id;
      var Mongo = $resource('/administrator/getuser/:id', {id: '@_id'});
      Mongo.get({id: currentId}, function (user) {
        userInfo = user;
      });
      $scope.resetPassword = function () {
        console.log(userInfo);
        if ($scope.current != undefined) {
          if ($scope.current.psw != undefined && $scope.current.newPsw
              != undefined && $scope.current.renewPsw != undefined) {
            var hash = hex_md5($scope.current.psw);
            var hash2 = hex_md5("c7vq89kh" + hash);
            console.log(hash2);
            if (hash2 != userInfo.password) {
              $scope.current.psw = '';
              alert('原密码错误，请重新输入');
            } else if ($scope.current.newPsw != $scope.current.renewPsw) {
              $scope.current.newPsw = '';
              $scope.current.renewPsw = '';
              alert('新密码不一致，请重新输入');
            } else {
              var hash_new = hex_md5($scope.current.newPsw);
              var hash_new2 = hex_md5("c7vq89kh" + hash_new);
              userInfo.password = hash_new2;
              var updatePsw=$resource('/administrator/update');
              updatePsw.save(userInfo, function () {
                alert('密码修改成功');
                $location.path('/sendUsers');
              })
            }
          }
        }
      };
    } else {
      window.location = "/send";
    }
  }]);
//修改商户信息
app.controller('EditUserCtrl', ['$scope', '$resource', '$location', '$routeParams', '$cookies',
      function ($scope, $resource, $location, $routeParams, $cookies) {
        var username = $cookies.get('username');
        if (username == 'admin') {
          $scope.name = "editClient";
          $scope.title = '修改商户信息';
          $scope.disabled = true;
          function randomWord(randomFlag) {
            var str = "",
                arr = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a',
                  'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm',
                  'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y',
                  'z'];
            // 随机产生
            for (var i = 0; i < randomFlag; i++) {
              var pos = Math.round(Math.random() * (arr.length - 1));
              str += arr[pos];
            }
            return str;
          }

          $scope.createSecretKey = function () {
            $scope.secretKey = randomWord(15);
          };
          $scope.options = [
            {key: '待使用', id: '0'},
            {key: '合同期内', id: '1'},
            {key: '合同过期', id: '2'}
          ];
          var Mongo = $resource('/manage/user/:id', {id: '@_id'});
          Mongo.get({id: $routeParams.id}, function (user) {
            $scope.appId = user.appId;
            $scope.secretKey = user.secretKey;
            $scope.checkContent = user.checkContent;
            $scope.status = $scope.options[user.status];
            $scope.remark = user.remark;
          });
          var MongoEdit = $resource('/manage');
          $scope.save = function () {
            if ($scope.status == undefined) {
              alert('请选择商户状态');
              return;
            }
            if ($scope.secretKey != '' && $scope.status != undefined) {
              var userInfo = {
                name: $scope.name,
                _id: $routeParams.id,
                appId: $scope.appId,
                secretKey: $scope.secretKey,
                status: $scope.status.id,
                checkContent: $scope.checkContent,
                remark: $scope.remark
              };
              MongoEdit.save(userInfo, function (data) {
                if (data.errNum == 1) {
                  alert(data.message);
                } else {
                  alert('商户信息修改成功');
                  $location.path('/userinfo');
                }
              });
            }
          };
        } else {
          window.location = "/send";
        }
      }]);
//合同期内商户
app.controller('UsableCtrl', ['$scope', '$resource', '$location', '$cookies',
  function ($scope, $resource, $location, $cookies) {
    var username = $cookies.get('username');
    if (username == 'admin') {
      $('.searchForm').hide();
      $scope.name = 'usableClient';
      $scope.title = '合同期内商户信息';
      $scope.first = true;
      $scope.pages = 0;
      $scope.pageSize = 15;
      $scope.p_current = 1;
      $scope.pageList = [];
      var Mongo = $resource('/manage');
      //第一次进入页面
      setData($scope.name, $scope.pageSize, $scope.p_current, $scope.first,
          $scope.content);
      //设置分页
      function setData(name, pageSize, p_current, first, content) {
        var postParams = {
          "name": name,
          "pageSize": pageSize,
          "p_current": p_current,
          "content": content
        };
        Mongo.save(postParams, function (data) {
          $scope.pages = Math.ceil(data.count / $scope.pageSize);
          $scope.items = data.result;
          if (first) {
            $scope.selectPage(p_current);
            $scope.first = false;
          }
        });
      }

      //点击页码跳转
      $scope.selectPage = function (page) {
        //不能小于1大于最大
        if (page < 1 || page > $scope.pages) {
          return;
        }
        //最多显示分页数5
        var newpageList = [];
        if (page < 3) {
          if ($scope.pages < 6) {
            for (var i = 0; i < $scope.pages; i++) {
              newpageList.push(i + 1);
            }
          } else {
            for (var i = 0; i < 5; i++) {
              newpageList.push(i + 1);
            }
          }
        } else {
          for (var i = (page - 3);
              i < ((page + 2) > $scope.pages ? $scope.pages : (page + 2));
              i++) {
            newpageList.push(i + 1);
          }
        }
        $scope.pageList = newpageList;
        $scope.p_current = page;
        if (!$scope.first) {
          setData($scope.name, $scope.pageSize, $scope.p_current, $scope.first,
              $scope.content);
        }
        $scope.isActivePage = function (page) {
          return $scope.p_current == page;
        };
      };
      //首页
      $scope.First = function () {
        $scope.selectPage(1);
      };
      //上一页
      $scope.Previous = function () {
        $scope.selectPage($scope.p_current - 1);
      };
      //下一页
      $scope.Next = function () {
        $scope.selectPage($scope.p_current + 1);
      };
      //尾页
      $scope.Last = function () {
        $scope.selectPage($scope.pages);
      };
    } else {
      window.location = "/send";
    }
  }]);
//合同过期商户
app.controller('ExpiredCtrl', ['$scope', '$resource', '$location', '$cookies',
  function ($scope, $resource, $location, $cookies) {
    var username = $cookies.get('username');
    if (username == 'admin') {
      $('.searchForm').hide();
      $scope.name = 'expiredClient';
      $scope.title = '合同过期商户信息';
      $scope.first = true;
      $scope.pages = 0;
      $scope.pageSize = 15;
      $scope.p_current = 1;
      $scope.pageList = [];
      var Mongo = $resource('/manage');
      //第一次进入页面
      setData($scope.name, $scope.pageSize, $scope.p_current, $scope.first,
          $scope.content);
      //设置分页
      function setData(name, pageSize, p_current, first, content) {
        var postParams = {
          "name": name,
          "pageSize": pageSize,
          "p_current": p_current,
          "content": content
        };
        Mongo.save(postParams, function (data) {
          $scope.pages = Math.ceil(data.count / $scope.pageSize);
          $scope.items = data.result;
          if (first) {
            $scope.selectPage(p_current);
            $scope.first = false;
          }
        });
      }

      //点击页码跳转
      $scope.selectPage = function (page) {
        //不能小于1大于最大
        if (page < 1 || page > $scope.pages) {
          return;
        }
        //最多显示分页数5
        var newpageList = [];
        if (page < 3) {
          if ($scope.pages < 6) {
            for (var i = 0; i < $scope.pages; i++) {
              newpageList.push(i + 1);
            }
          } else {
            for (var i = 0; i < 5; i++) {
              newpageList.push(i + 1);
            }
          }
        } else {
          for (var i = (page - 3);
              i < ((page + 2) > $scope.pages ? $scope.pages : (page + 2));
              i++) {
            newpageList.push(i + 1);
          }
        }
        $scope.pageList = newpageList;
        $scope.p_current = page;
        if (!$scope.first) {
          setData($scope.name, $scope.pageSize, $scope.p_current, $scope.first,
              $scope.content);
        }
        $scope.isActivePage = function (page) {
          return $scope.p_current == page;
        };
      };
      //首页
      $scope.First = function () {
        $scope.selectPage(1);
      };
      //上一页
      $scope.Previous = function () {
        $scope.selectPage($scope.p_current - 1);
      };
      //下一页
      $scope.Next = function () {
        $scope.selectPage($scope.p_current + 1);
      };
      //尾页
      $scope.Last = function () {
        $scope.selectPage($scope.pages);
      };
    } else {
      window.location = "/send";
    }
  }]);
//待使用商户
app.controller('UnusedCtrl', ['$scope', '$resource', '$location', '$cookies',
  function ($scope, $resource, $location, $cookies) {
    var username = $cookies.get('username');
    if (username == 'admin') {
      $('.searchForm').hide();
      $scope.name = 'unusedClient';
      $scope.title = '待使用商户信息';
      $scope.first = true;
      $scope.pages = 0;
      $scope.pageSize = 15;
      $scope.p_current = 1;
      $scope.pageList = [];
      var Mongo = $resource('/manage');
      //第一次进入页面
      setData($scope.name, $scope.pageSize, $scope.p_current, $scope.first,
          $scope.content);
      //设置分页
      function setData(name, pageSize, p_current, first, content) {
        var postParams = {
          "name": name,
          "pageSize": pageSize,
          "p_current": p_current,
          "content": content
        };
        Mongo.save(postParams, function (data) {
          $scope.pages = Math.ceil(data.count / $scope.pageSize);
          $scope.items = data.result;
          if (first) {
            $scope.selectPage(p_current);
            $scope.first = false;
          }
        });
      }

      //点击页码跳转
      $scope.selectPage = function (page) {
        //不能小于1大于最大
        if (page < 1 || page > $scope.pages) {
          return;
        }
        //最多显示分页数5
        var newpageList = [];
        if (page < 3) {
          if ($scope.pages < 6) {
            for (var i = 0; i < $scope.pages; i++) {
              newpageList.push(i + 1);
            }
          } else {
            for (var i = 0; i < 5; i++) {
              newpageList.push(i + 1);
            }
          }
        } else {
          for (var i = (page - 3);
              i < ((page + 2) > $scope.pages ? $scope.pages : (page + 2));
              i++) {
            newpageList.push(i + 1);
          }
        }
        $scope.pageList = newpageList;
        $scope.p_current = page;
        if (!$scope.first) {
          setData($scope.name, $scope.pageSize, $scope.p_current, $scope.first,
              $scope.content);
        }
        $scope.isActivePage = function (page) {
          return $scope.p_current == page;
        };
      };
      //首页
      $scope.First = function () {
        $scope.selectPage(1);
      };
      //上一页
      $scope.Previous = function () {
        $scope.selectPage($scope.p_current - 1);
      };
      //下一页
      $scope.Next = function () {
        $scope.selectPage($scope.p_current + 1);
      };
      //尾页
      $scope.Last = function () {
        $scope.selectPage($scope.pages);
      };
    } else {
      window.location = "/send";
    }
  }]);

//上行短信发送情况
app.controller('UpMessageCtrl', ['$scope', '$resource', '$location', '$cookies',
  function ($scope, $resource, $location, $cookies) {
    var username = $cookies.get('username');
    if (username == 'admin') {
      $scope.title = "上行短信发送情况";
      $scope.first = true;
      $scope.pages = 0;
      $scope.pageSize = 15;
      $scope.p_current = 1;
      $scope.pageList = [];
      var Mongo = $resource('/upMessage');
      //第一次进入页面
      setData($scope.pageSize, $scope.p_current, $scope.first, $scope.content);
      //设置分页
      function setData(pageSize, p_current, first, content) {
        var postParams = {
          "pageSize": pageSize,
          "p_current": p_current,
          "content": content
        };
        Mongo.save(postParams, function (data) {
          $scope.pages = Math.ceil(data.count / $scope.pageSize);
          $scope.items = data.result;
          if (first) {
            $scope.selectPage(p_current);
            $scope.first = false;
          }
        });
      }

      //点击页码跳转
      $scope.selectPage = function (page) {
        //不能小于1大于最大
        if (page < 1 || page > $scope.pages) {
          return;
        }
        //最多显示分页数5
        var newpageList = [];
        if (page < 3) {
          if ($scope.pages < 6) {
            for (var i = 0; i < $scope.pages; i++) {
              newpageList.push(i + 1);
            }
          } else {
            for (var i = 0; i < 5; i++) {
              newpageList.push(i + 1);
            }
          }
        } else {
          for (var i = (page - 3);
              i < ((page + 2) > $scope.pages ? $scope.pages : (page + 2));
              i++) {
            newpageList.push(i + 1);
          }
        }
        $scope.pageList = newpageList;
        $scope.p_current = page;
        if (!$scope.first) {
          setData($scope.pageSize, $scope.p_current, $scope.first,
              $scope.content);
        }
        $scope.isActivePage = function (page) {
          return $scope.p_current == page;
        };
      };
      //首页
      $scope.First = function () {
        $scope.selectPage(1);
      };
      //上一页
      $scope.Previous = function () {
        $scope.selectPage($scope.p_current - 1);
      };
      //下一页
      $scope.Next = function () {
        $scope.selectPage($scope.p_current + 1);
      };
      //尾页
      $scope.Last = function () {
        $scope.selectPage($scope.pages);
      };
    } else {
      window.location = "/send";
    }
  }]);

//推送用户管理
app.controller('SendUsersCtrl',['$scope', '$resource', '$location', '$cookies',
  function ($scope, $resource, $location, $cookies){
    var username=$cookies.get('username');
    if(username == 'admin'){
      $scope.title = '所有推送用户信息';
      $scope.first = true;
      $scope.pages = 0;
      $scope.pageSize = 15;
      $scope.p_current = 1;
      $scope.pageList = [];
      var Mongo=$resource('/administrator');
      //第一次进入页面
      setData($scope.pageSize, $scope.p_current, $scope.first,
          $scope.content);
      //设置分页
      function setData(pageSize, p_current, first, content) {
        var params = {
          "pageSize": pageSize,
          "p_current": p_current,
          "content": content
        };
        Mongo.get(params, function (data) {
          $scope.pages = Math.ceil(data.count / $scope.pageSize);
          $scope.items = data.result;
          if (first) {
            $scope.selectPage(p_current);
            $scope.first = false;
          }
        });
      }

      //点击页码跳转
      $scope.selectPage = function (page) {
        //不能小于1大于最大
        if (page < 1 || page > $scope.pages) {
          return;
        }
        //最多显示分页数5
        var newpageList = [];
        if (page < 3) {
          if ($scope.pages < 6) {
            for (var i = 0; i < $scope.pages; i++) {
              newpageList.push(i + 1);
            }
          } else {
            for (var i = 0; i < 5; i++) {
              newpageList.push(i + 1);
            }
          }
        } else {
          for (var i = (page - 3);
              i < ((page + 2) > $scope.pages ? $scope.pages : (page + 2));
              i++) {
            newpageList.push(i + 1);
          }
        }
        $scope.pageList = newpageList;
        $scope.p_current = page;
        if (!$scope.first) {
          setData($scope.pageSize, $scope.p_current, $scope.first,
              $scope.content);
        }
        $scope.isActivePage = function (page) {
          return $scope.p_current == page;
        };
      };
      //首页
      $scope.First = function () {
        $scope.selectPage(1);
      };
      //上一页
      $scope.Previous = function () {
        $scope.selectPage($scope.p_current - 1);
      };
      //下一页
      $scope.Next = function () {
        $scope.selectPage($scope.p_current + 1);
      };
      //尾页
      $scope.Last = function () {
        $scope.selectPage($scope.pages);
      };
      //按商户号、用户名等进行模糊查询
      $scope.search = function () {
        $scope.p_current = 1;
        $scope.first = true;
        setData($scope.pageSize, $scope.p_current, $scope.first,
            $scope.content);
      };
    }else{
      window.location = "/send";
    }
  }]);
//添加推送用户
app.controller('AddSendUserCtrl',['$scope', '$resource', '$location', '$cookies',
  function ($scope, $resource, $location, $cookies){
    var username = $cookies.get('username');
    if(username == 'admin'){
      $scope.title = '新增推送用户';
      var getAppIDList=$resource('/manage/getappids');
      getAppIDList.get(function(data){
        var result=data.result;
        console.log(result);
        $scope.options=result;
      });
      $scope.save=function(){
        var susername=$scope.susername;
        var password=$scope.password;
        var appid=$scope.appId.appId;
        if(susername!='' && password!='' && appid !=''){
          var addSuser=$resource('/administrator/addsuser');
          var postParams={username:susername,password:password,appid:appid};
          console.log(postParams);
          addSuser.save(postParams,function(data){
            if (data.errNum == 0) {
              alert(data.message);
              $scope.susername='';
            } else {
              alert("添加成功");
              $location.path('/sendUsers');
            }
          })
        }else{
          alert("所有内容必填");
        }
      };
    }else{
      window.location = "/send";
    }
  }]);
//删除推送用户
app.controller('DeleteSendUserCtrl',['$scope', '$resource', '$location', '$routeParams', '$cookies',
  function ($scope, $resource, $location, $routeParams, $cookies) {
    var username = $cookies.get('username');
    if (username == 'admin') {
      var Mongo=$resource('/administrator/getuser/:id');
      Mongo.get({id:$routeParams.id},function(user){
        $scope.user=user;
      });
      $scope.delete=function(){
        var deleteSendUser=$resource('/administrator/:id');
        deleteSendUser.delete({id:$routeParams.id},function(){
          alert("删除成功");
          $location.path('/sendUsers');
        })
      };
    }else{
      window.location = "/send";
    }
  }]);


